//
//  ZhuanZhangViewController.m
//  1243
//
//  Created by 我的 on 16/4/6.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ZhuanZhangViewController.h"
#import "ReadLoginData.h"
#import "NetWorkTools.h"

@interface ZhuanZhangViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
{
    NSString *message;
    NSDictionary *loginDict;
}
@property (weak, nonatomic) IBOutlet UILabel *YuELabel;
@property (weak, nonatomic) IBOutlet UITextField *ZhuanZhangJinE;
@property (weak, nonatomic) IBOutlet UITextField *PhoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *BuyPassWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *NameTxtField;

@property(nonatomic,strong)NSDictionary *UserDict;
@end


@implementation ZhuanZhangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"转账";
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    self.YuELabel.text = [NSString stringWithFormat:@"%.2f 元",[[dict objectForKey:@"Money"] floatValue]];
    
    [self setup];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

- (void)setup
{
   
    
    self.PhoneNumber.delegate = self;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self clearTextField];
}
- (IBAction)didClickZhuanZhangButton:(id)sender {
   
    
    NSString *fromUserNum = [NSString stringWithFormat:@"%@",[[ReadLoginData readLoginData] objectForKey:@"Number"]] ;
 
    
    if (![self.PhoneNumber.text length] || ![self.ZhuanZhangJinE.text length] || ![self.BuyPassWordTextField.text length]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请您输入完整信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }else {
        
        if (!self.UserDict) {
            
        }else{
            
            NSDictionary *dict = @{
                                   @"ToUserNum":self.UserDict[@"Number"],
                                   @"FromUserNum":fromUserNum,
                                   @"TurnMoney":self.ZhuanZhangJinE.text,
                                   @"FromPassword":self.BuyPassWordTextField.text,
                                   };
            [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"TURNACCOUNT" completeBlock:^(id object) {
                NSLog(@"%@",object)
                
                message = [object objectForKey:@"Message"];
                if ([[object objectForKey:@"Message"] isEqualToString:@"转账成功!"]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alert.tag = 1111;
                    [alert show];
                }else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alert.tag = 2222;
                    [alert show];
                    
                }
                
            }];
            
        }
            
        }     
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1111) {
        [self loadUserData];
         [self cleanTextField];
    }else if(alertView.tag==3333){
        self.PhoneNumber.text = @"";

    }else{
        self.BuyPassWordTextField.text = @"";
    }
}


- (void)getSearchUserData
{
    NSDictionary *dict = @{
              @"Number":[NSString stringWithFormat:@"%@",self.PhoneNumber.text]
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"NUMUSERQUERY" completeBlock:^(id object) {
        NSLog(@"%@",object)
        
        if ([object[@"Message"] isEqualToString:@"查询成功"]) {
            NSString *DataString = [object objectForKey:@"BackData"];
            
            NSArray *array = [NetWorkTools arrayWithJsonString:DataString];
            
           self.UserDict= [array firstObject];
            self.NameTxtField.text = [NSString stringWithFormat:@"%@",self.UserDict[@"RealName"]];
            
            
        }else{
            
//            self.NameTxtField.text = @"";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户不存在,请您重新输入" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            alert.tag = 1111;
            [alert show];
        }

        }];
}

- (void)loadUserData {
    
    
        NSDictionary *dict = @{
                               @"PhoneNum":[ReadLoginData readLoginData][@"UserName"],
                               @"Password":[ReadLoginData readLoginData][@"Password"]
                               };
        
        [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"USERLOGIN" completeBlock:^(id object) {
            
            NSLog(@"%@",object);
            
            loginDict = [NetWorkTools dictionaryWithJsonString:[object objectForKey:@"BackData"]];
//            Money
            self.YuELabel.text = [NSString stringWithFormat:@"%.2f 元",[[loginDict objectForKey:@"Money"] floatValue]];

        }];
}



- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField.text length] == 11) {
        [self getSearchUserData];
    }else{
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的账号格式不正确，请重新输入！" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        alert.tag = 3333;
        [alert show];
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        if (string.length == 0) return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        
        if (existedLength - selectedLength + replaceLength >= 12) {
            return NO;
        }
    
        return YES;
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.PhoneNumber) {
        if (textField.text.length >=12) {
            textField.text = [textField.text substringToIndex:12];
        }
    }
}


- (void)cleanTextField {
    self.ZhuanZhangJinE.text = @"";
    self.PhoneNumber.text = @"";
    self.BuyPassWordTextField.text = @"";
    self.NameTxtField.text = @"";
}

- (void)clearTextField {
    [self.ZhuanZhangJinE resignFirstResponder];
    [self.PhoneNumber resignFirstResponder];
    [self.BuyPassWordTextField resignFirstResponder];
}
@end
